package com.sgb.kjwl.viewmodel.self.company

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.sgb.kjwl.api.NewAPIManager
import com.sgb.kjwl.api.service.IMServiceAPI.GetUrlSettingEntity
import com.sgb.kjwl.model.entity.self.AgentEnquiryEntity
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.api.ICallBack
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.ListResponse
import com.swgk.core.util.PickSelectEntity
import retrofit2.Call

class EnquiryViewModel : BaseViewModel() {

    private val mTitles = arrayListOf("商品", "租赁", "物流", "劳务", "工种", "其他")

    var titles = MutableLiveData<List<PickSelectEntity<Any>>>()

    private var page = 1//分页加载起始页
    private var limit = 10 //每页加载数量
    private var map: HashMap<String, Any> = HashMap()

    val shopList = MutableLiveData<List<AgentEnquiryEntity>>()

    val readPosition = MutableLiveData<Int>()

    fun getTitle(hasPermission: Boolean) {
        var list = mTitles.mapIndexed { index, s ->
            PickSelectEntity<Any>().apply {
                if (index == 0 && !hasPermission) {
                    no = "-1"
                    projectscale = s
                } else {
                    no = "".plus(index + 1)
                    projectscale = s
                }
            }
        }
        titles.postValue(list)
    }

    //获取商品询价列表
    fun getShopEnquiryList(isLoadMore: Boolean) {
        page = if (isLoadMore) page + 1 else 1
        map["page"] = page
        map["limit"] = limit
        APIManager.getInstance().userAPI.getEnquiryShopList(map).enqueue(object : ICallBack<BaseEntity<ListResponse<List<AgentEnquiryEntity>>>>(true, true) {
            override fun onSuccess(data: BaseEntity<ListResponse<List<AgentEnquiryEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null) {
                    shopList.postValue(data.data.list)
                } else {
                    shopList.postValue(ArrayList())
                }
            }

            override fun onFailure(
                    call: Call<BaseEntity<ListResponse<List<AgentEnquiryEntity>>>>,
                    t: Throwable
            ) {
                super.onFailure(call, t)
                shopList.postValue(ArrayList())
            }
        })
    }


    //保存用户已读数据
    fun saveUserReadAgent(id: String, position: Int) {
        map = HashMap()
        map["businessNo"] = id
        APIManager.getInstance().userAPI.saveUserReadAgent(map).enqueue(object : ICallBack<BaseEntity<*>>() {
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null) {
                    readPosition.postValue(position)
                }
            }
        })
    }


    /**
     * 询价详情  position:0 我收到的询价 1 我发起的询价
     */
    fun getPriceCheckDetail(context: Context, id: String, state: String, position: Int) {
        val s = if (position == 0) "pensonalQuoteDetail" else "pensonalEnpuiryDetail"
        NewAPIManager.getInstance().productInquiryAPI.getUrlSetting(s)
                .enqueue(object : ICallBack<BaseEntity<GetUrlSettingEntity>>(true, true) {
                    override fun onSuccess(data: BaseEntity<GetUrlSettingEntity>) {
                        if (data?.data != null) {
                            val url = data.data.url + "?no=" + id
                            WebViewX5HostActivity.start(context, url, id, "2", state)
                        }
                    }
                })
    }


}